package com.zyw.zhuangdian.controller;

import com.alibaba.fastjson.JSONObject;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.in.CarouselDtoIn;
import com.zyw.zhuangdian.pojo.out.CarouselDtoOut;
import com.zyw.zhuangdian.service.CarouselService;
import com.zyw.zhuangdian.util.Assert;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import com.zyw.zhuangdian.util.RespUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/carousel")
@RequiredArgsConstructor
@Slf4j
public class CarouselController {
    private final CarouselService CarouselService;

    /**
     * 增加轮播图
     * @param json
     * @return
     */
    @RequestMapping(value = "addCarousel",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> addCarousel(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"title","标题不能为空");
        Assert.hasKeyAndValue(json,"picture","图片不能为空");
        Assert.hasKeyAndValue(json,"type","类型不能为空");
        Assert.hasKeyAndValue(json,"toId","关联Id不能为空");

        CarouselDtoIn in = BeanConvertUtil.covertBean(json, CarouselDtoIn.class);
        Resp<Void> resp = CarouselService.addCarousel(in);
        return RespUtil.OK(resp);
    }

    /**
     * 修改轮播图
     * @param json
     * @return
     */
    @RequestMapping(value = "updateCarousel",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> updateCarousel(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"cid","Id不能为空");
        Assert.hasKeyAndValue(json,"title","标题不能为空");
        Assert.hasKeyAndValue(json,"picture","图片不能为空");
        Assert.hasKeyAndValue(json,"type","类型不能为空");
        Assert.hasKeyAndValue(json,"toId","关联Id不能为空");

        CarouselDtoIn in = BeanConvertUtil.covertBean(json, CarouselDtoIn.class);
        Resp<Void> resp = CarouselService.updateCarousel(in);
        return RespUtil.OK(resp);
    }

    /**
     * 删除轮播图
     * @param json
     * @return
     */
    @RequestMapping(value = "deleteCarousel",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> deleteCarousel(@RequestBody JSONObject json){
        Assert.hasKeyAndValue(json,"cid","Id不能为空");

        CarouselDtoIn in = BeanConvertUtil.covertBean(json, CarouselDtoIn.class);
        Resp<Void> resp = CarouselService.deleteCarousel(in);
        return RespUtil.OK(resp);
    }

    /**
     * 列出轮播图
     * @param json (key)
     * @return
     */
    @RequestMapping(value = "listCarousel",method = RequestMethod.POST)
    public ResponseEntity<Resp<?>> listCarousel(@RequestBody JSONObject json){

        CarouselDtoIn in = BeanConvertUtil.covertBean(json, CarouselDtoIn.class);
        Resp<List<CarouselDtoOut>> resp = CarouselService.listCarousels(in);
        return RespUtil.OK(resp);
    }
}
